package com.scau402.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.scau402.common.core.domain.entity.SysUser;
import com.scau402.common.utils.SecurityUtils;
import com.scau402.common.utils.StringUtils;
import com.scau402.common.utils.code.ClientCodeUtils;
import com.scau402.common.utils.code.ProcessorCodeUtils;
import com.scau402.system.domain.SysTenant;
import com.scau402.system.service.ISysTenantService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.scau402.common.annotation.Log;
import com.scau402.common.core.controller.BaseController;
import com.scau402.common.core.domain.AjaxResult;
import com.scau402.common.enums.BusinessType;
import com.scau402.system.domain.WmsBasicProcessor;
import com.scau402.system.service.IWmsBasicProcessorService;
import com.scau402.common.utils.poi.ExcelUtil;
import com.scau402.common.core.page.TableDataInfo;

/**
 * 加工厂Controller
 *
 * @author ruoyi
 * @date 2023-06-26
 */
@RestController
@RequestMapping("/system/processor")
@Slf4j
public class WmsBasicProcessorController extends BaseController {
    @Autowired
    private IWmsBasicProcessorService wmsBasicProcessorService;

    @Autowired
    private ISysTenantService tenantService;

    /**
     * 查询加工厂列表
     */
    @PreAuthorize("@ss.hasPermi('system:processor:list')")
    @GetMapping("/list")
    public TableDataInfo list(WmsBasicProcessor wmsBasicProcessor) {
        log.info("查询加工厂列表, 参数: {}", wmsBasicProcessor);
        startPage();
        List<WmsBasicProcessor> list = wmsBasicProcessorService.selectWmsBasicProcessorList(wmsBasicProcessor);
        log.debug("查询加工厂列表, 结果: {}", list);
        return getDataTable(list);
    }

    /**
     * 导出加工厂列表
     */
    @PreAuthorize("@ss.hasPermi('system:processor:export')")
    @Log(title = "加工厂", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmsBasicProcessor wmsBasicProcessor) {
        log.info("导出加工厂列表, 参数: {}", wmsBasicProcessor);
        List<WmsBasicProcessor> list = wmsBasicProcessorService.selectWmsBasicProcessorList(wmsBasicProcessor);
        log.debug("导出加工厂列表, 结果: {}", list);
        ExcelUtil<WmsBasicProcessor> util = new ExcelUtil<WmsBasicProcessor>(WmsBasicProcessor.class);
        util.exportExcel(response, list, "加工厂数据");
    }

    /**
     * 获取加工厂详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:processor:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        log.info("获取加工厂详细信息, 参数: {}", id);
        return success(wmsBasicProcessorService.selectWmsBasicProcessorById(id));
    }

    /**
     * 新增加工厂
     */
    @PreAuthorize("@ss.hasPermi('system:processor:add')")
    @Log(title = "加工厂", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody WmsBasicProcessor wmsBasicProcessor) {
        log.info("新增加工厂, 参数: {}", wmsBasicProcessor);
        wmsBasicProcessor.setCreateBy(getUsername());
        return toAjax(wmsBasicProcessorService.insertWmsBasicProcessor(wmsBasicProcessor));
    }

    /**
     * 修改加工厂
     */
    @PreAuthorize("@ss.hasPermi('system:processor:edit')")
    @Log(title = "加工厂", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody WmsBasicProcessor wmsBasicProcessor) {
        log.info("修改加工厂, 参数: {}", wmsBasicProcessor);
        return toAjax(wmsBasicProcessorService.updateWmsBasicProcessor(wmsBasicProcessor));
    }

    /**
     * 删除加工厂
     */
    @PreAuthorize("@ss.hasPermi('system:processor:remove')")
    @Log(title = "加工厂", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        log.info("删除加工厂, 参数: {}", ids);
        return toAjax(wmsBasicProcessorService.deleteWmsBasicProcessorByIds(ids));
    }


    /**
     * 获取加工厂编码
     *
     * @return
     * @throws InterruptedException
     */
    @PreAuthorize("@ss.hasPermi('system:processor:add')")
    @GetMapping("/getProcessorCode")
    public String getProcessorCode() throws InterruptedException {
        // 返回租户编码
        return wmsBasicProcessorService.getNewCode(SecurityUtils.getLoginUser().getUser());
    }
}
